package org.nfyszny.zzzs.modules.listener;


import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.dto.message.MessageDTO;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.constant.enums.MessageTypeEnum;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.system.service.ISysUserService;
import org.nfyszny.zzzs.modules.constant.ExamineConstant;
import org.nfyszny.zzzs.modules.entity.ZzzsExamine;
import org.nfyszny.zzzs.modules.service.IZzzsExamineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName RedisListen
 * @Description RedisListen
 * @Author cl
 * @Date 2021-09-09 10:46
 */
@Slf4j
@Component
public class RedisListener implements MessageListener {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IZzzsExamineService examineService;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        String expiredKey = message.toString();

        log.info("失效的redis是:" + expiredKey);
        RedisSerializer<?> serializer = redisTemplate.getValueSerializer();
        String channel = String.valueOf(serializer.deserialize(message.getChannel()));
        String body = String.valueOf(serializer.deserialize(message.getBody()));
        log.info("channel===" + channel + "-----------------" + "body === " + body);
        //第一位是代表审批编号,第二位代表任务开始时间,第三位代表人,第四位代表备案类型,第二位代表事件
        String[] s = body.split("_");
        System.out.println(Arrays.toString(s));
        //key过期监听,在处理业务之前校验下自己业务的key和监听的key以及库号
        if (s.length == 5) {
            //检查状态是否为审批中
            if (examineService.checkStatus(s[0])) {
                if ("__keyevent@0__:expired".equals(channel) && s[4].equals("message")) {

                    //这里写需要处理的业务
                    MessageDTO messageDTO = new MessageDTO();
                    messageDTO.setFromUser("admin");
                    messageDTO.setToUser(userService.getById(s[2]).getUsername());
                    messageDTO.setIsMarkdown(true);
                    messageDTO.setTemplateCode("check_template");
                    messageDTO.setCategory(CommonConstant.MSG_CATEGORY_1);
                    messageDTO.setTitle("您有审批快到期了请尽快处理");
                    messageDTO.setType(MessageTypeEnum.XT.getType());
                    Map<String, Object> map = new HashMap<>();
                    map.put("title", "有未处理的" + s[3] + "审批");
                    System.out.println("s[2] = " + s[2]);
                    map.put("user", userService.getById(s[2]).getRealname());
                    map.put("time", s[1]);
                    messageDTO.setData(map);
                    sysBaseAPI.sendTemplateMessage(messageDTO);

                } else if ("__keyevent@0__:expired".equals(channel) && s[4].equals("exceed")) {
                    //获取审批记录
                    ZzzsExamine zzzsExamine = examineService.getById(s[0]);
                    zzzsExamine.setExamineStatus(ExamineConstant.StatusEnum.CHECK_TIMEOUT.getCode());
                    //将审批记录的状态设置为已超时
                    examineService.updateById(zzzsExamine);
                }
            }
        }

    }
}
